package com.JaMorant.SSM.thc.service.impl;

import com.JaMorant.SSM.exception.GgktException;
import com.JaMorant.SSM.model.thc.Chengshi;
import com.JaMorant.SSM.model.thc.GoodsConfig;
import com.JaMorant.SSM.thc.listener.GoodsConfigListener;
import com.JaMorant.SSM.thc.listener.GoodsConfigShiyongListener;
import com.JaMorant.SSM.thc.mapper.GoodsConfigMapper;
import com.JaMorant.SSM.thc.service.GoodsConfigService;
import com.JaMorant.SSM.vo.thc.DeliveryConfigVo;
import com.JaMorant.SSM.vo.thc.GoodsConfigShiyongVo;
import com.JaMorant.SSM.vo.thc.GoodsConfigVo;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 城市 服务实现类
 * </p>
 *
 * @author JaMorant
 * @since 2023-02-19
 */
@Service
public class GoodsConfigServiceImpl extends ServiceImpl<GoodsConfigMapper, GoodsConfig> implements GoodsConfigService {
    @Autowired
    private GoodsConfigListener goodsConfigListener;
    @Autowired
    private GoodsConfigShiyongListener goodsConfigShiyongListener;

    @Override
    public void exportData(HttpServletResponse response) {
        try {
            //设置下载信息
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("城市分类", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

            //查询课程分类表所有数据
            List<Chengshi> chengshiList = new ArrayList<>();
//                List<Chengshi> chengshiList = baseMapper.selectList(null);

            //List<Chengshi> ---  List<DeliveryConfigVo>
            List<DeliveryConfigVo> deliveryConfigVoList = new ArrayList<>();
            for (Chengshi chengshi : chengshiList) {
                DeliveryConfigVo deliveryConfigVo = new DeliveryConfigVo();
//                deliveryConfigVo.setId(chengshi.getId());
//                deliveryConfigVo.setParentId(chengshi.getParentId());
                BeanUtils.copyProperties(chengshi, deliveryConfigVo);
                deliveryConfigVoList.add(deliveryConfigVo);
            }

            //EasyExcel写操作
            EasyExcel.write(response.getOutputStream(), DeliveryConfigVo.class)
                    .sheet("城市分类")
                    .doWrite(deliveryConfigVoList);
        } catch (Exception e) {
            throw new GgktException(20001, "导出失败");
        }
    }


    //课程分类导入
    @Override
    public void importData(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(),
                    GoodsConfigVo.class,
                    goodsConfigListener).sheet().doRead();
        } catch (IOException e) {
            throw new GgktException(20001, "导入失败");
        }
    }

    /**
     * 计算试用装商品价格
     *
     * @param file
     */
    @Override
    public void calGoodsPrice(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(),
                    GoodsConfigShiyongVo.class,
                    goodsConfigShiyongListener).sheet().doRead();
        } catch (IOException e) {
            throw new GgktException(20001, "导入失败");
        }

    }

    @Override
    public void calGoodsPrice2() {
        Map<String, String> map = new HashMap<>();
        map.put("XJH-K001", "小家伙纸尿裤好芯好报NB/S50");
        map.put("XJH-K002", "小家伙纸尿裤好芯好报M46");
        map.put("XJH-K003", "小家伙纸尿裤好芯好报L42");
        map.put("XJH-K004", "小家伙尿裤好芯好报XL38");
        map.put("XJH-K005", "小家伙纸尿裤好芯好报XXL36");
        map.put("XJH-L003", "小家伙拉拉裤好芯好报L42");
        map.put("XJH-L004", "小家伙拉拉裤好芯好报XL38");
        map.put("XJH-L005", "小家伙拉拉裤好芯好报XXL36");
        map.put("XJH-L006", "小家伙拉拉裤好芯好报XXXL34");
        map.put("XJH-L007", "小家伙拉拉裤好芯好报XXXXL32");
        map.put("XJH-K100", "小家伙纸尿裤全芯全意NB66");
        map.put("XJH-K101", "小家伙纸尿裤全芯全意S60");
        map.put("XJH-K102", "小家伙纸尿裤全芯全意M54");
        map.put("XJH-K103", "小家伙纸尿裤全芯全意L50");
        map.put("XJH-K104", "小家伙纸尿裤全芯全意XL46");
        map.put("XJH-K105", "小家伙纸尿裤全芯全意XXL42");
        map.put("XJH-L103", "小家伙拉拉裤全芯全意L50");
        map.put("XJH-L104", "小家伙拉拉裤全芯全意XL46");
        map.put("XJH-L105", "小家伙拉拉裤全芯全意XXL42");
        map.put("XJH-L106", "小家伙拉拉裤全芯全意XXXL38");
        map.put("XJH-L107", "小家伙拉拉裤全芯全意XXXXL36");

        for (Map.Entry<String, String> stringStringEntry : map.entrySet()) {
            String key = stringStringEntry.getKey();
            String value = stringStringEntry.getValue();

            GoodsConfig goodsConfig = new GoodsConfig();
            goodsConfig.setGoodsNo(key+"-15");
            goodsConfig.setGoodsName("试用-"+value+"-15片");
            BigDecimal bigDecimal = new BigDecimal(0.5);
            BigDecimal bigDecimal2 = new BigDecimal(15);
            BigDecimal multiply = bigDecimal.multiply(bigDecimal2);
            goodsConfig.setPrice(multiply);
            //添加
            baseMapper.insert(goodsConfig);
        }



    }


}
